package io.hilo.example.demo.common;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CommonsRequestLoggingFilter;
/**
 * 
 * @author zollty
 * @since 2020年11月20日
 */
@Configuration
public class HttpRequestLoggingFilter extends CommonsRequestLoggingFilter {
    private static long beforeTime;
    private static long afterTime;
 
    public HttpRequestLoggingFilter() {
        super.setIncludeClientInfo(true);
        super.setIncludeHeaders(true);
        super.setIncludePayload(true);
        super.setMaxPayloadLength(2000);
        super.setIncludeQueryString(true);
    }
 
    @Override
    protected boolean shouldLog(HttpServletRequest request) {
//        String requestURI = request.getRequestURI();
//        // Checks if request matches /api/calendarCompletionDate
//        boolean shouldLog = requestURI.matches("^/api\\/calendarCompletionDate$");
// 
//        if (shouldLog) {
//            long currentUserId = ShiroUtils.getUserId();
//            String method = request.getMethod();
//            super.setBeforeMessagePrefix("Before request [" + method + "," + "currentUserId:" + currentUserId + ","); //Default is just Before request
//            super.setAfterMessagePrefix("After request [" + method + "," + "currentUserId:" + currentUserId + ",");   //Default is just After request
//        }
//        return shouldLog;
        return true;
    }
 
    @Override
    protected void beforeRequest(HttpServletRequest request, String message) {
        logger.info("-------------------------------------------------------------");
        beforeTime = System.currentTimeMillis();
        super.beforeRequest(request, message);
    }
 
    @Override
    protected void afterRequest(HttpServletRequest request, String message) {
        afterTime = System.currentTimeMillis();
        logger.info("afterRequest: Time taken: " + (afterTime-beforeTime) + " in milliseconds");
        super.afterRequest(request, message);
        logger.info("-------------------------------------------------------------");
    }


}
